<template>
  <div class="loginContainer">
    <el-form ref="loginForm" :rules="loginRules" :model="loginForm" class="login-form">
      <h3 class="title">星云工资管理系统</h3>
      <el-form-item prop="username">
        <el-input type="text" auto-complete="off" placeholder="用户名" v-model.trim="loginForm.username" @keyup.enter.native="login">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input show-password type="password" auto-complete="off" placeholder="密码" v-model.trim="loginForm.password" @keyup.enter.native="login">
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-checkbox style="margin:0px 0px 25px 0px;" v-model="loginForm.rememberMe">记住密码</el-checkbox>
      <el-form-item>
        <el-button :loading="loading" type="primary" style="width:100%" @click.native="login">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <span style="float:right">还未注册？</router-link><router-link to="/register">立即注册</router-link> | <router-link to="/forgot">忘记密码?</router-link></span>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Cookies from 'js-cookie'
import {encrypt, decrypt} from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      loading: false,
      loginForm: {
        username: "",
        password: "",
        rememberMe: false
      },
      loginRules: {
        username: [{required: true, trigger: 'blur', message: '用户名不能为空'}],
        password: [{required: true, trigger: 'blur', message: '密码不能为空'}]
      },
      redirect: undefined
    }
  },
  created() {
    this.getCookies()
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    login() {
      this.$refs.loginForm.validate(valid => {
        if (valid && !this.loading) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set('username', this.loginForm.username, {expires: 30});
            Cookies.set('password', encrypt(this.loginForm.password), {expires: 30});
            Cookies.set('rememberMe', this.loginForm.rememberMe, {expires: 30});
          } else {
            Cookies.remove('username');
            Cookies.remove('password');
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch('user/Login', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" });
          }).catch((error) => {
            console.log(error)
            this.loading = false;
          })
        }
      })
    },
    getCookies() {
      const username = Cookies.get('username')
      const password = Cookies.get('password')
      const rememberMe = Cookies.get('rememberMe')

      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    }
  }
};
</script>

<style lang="scss">

</style>